
import {
    CTileLayer,
    CWMTS,
    CWMS,
    CWMTSTileGrid,
    CgetWidth,
    Cget,
    CXYZ
} from '../../modules/ol'


const CLOUD_API = (window as any)['env'];
interface Props {
    Source: any
}


export default class Layer {


    init = () => new CTileLayer({
        zIndex: 1,
        source: WMTS()
    })
    /**
     * wmts 数据格式的参数
     * @param param0 数据参数
     * @returns 
     */
    TileLayer = ({ Source }: Props) => {
        let layers: any[] = [];

        if (!Array.isArray(Source.url)) return layers;

        Source.url.forEach((r: any, i: any) => {
            layers[i] = new CTileLayer({
                zIndex: Source.zIndex[i] || 2,
                source: new CWMTS({
                    url: r || '',
                    layer: Source.layer[i] || '',
                    matrixSet: Source.matrixSet,
                    format: Source.format,
                    style: Source.style,
                    tileGrid: new CWMTSTileGrid({
                        // tileSize: [256, 256],
                        // extent: [-180.0, -90.0, 180.0, 90.0],
                        origin: [-180.0, 90.0],
                        resolutions: Source.resolutions,
                        matrixIds: Source.matrixIds,
                    }),
                    wrapX: true
                })
            })
        });
        return layers

    }
    /**
     * wms 数据格式的参数
     * @param param0 数据参数
     * @returns 
     */
    TileCWMS = ({ Source }: Props) => {

        let layers: any = [];
        if (!Array.isArray(Source.url)) return layers;

        Source.url.forEach((r: any, i: any) => {
            layers[i] = new CTileLayer({
                zIndex: Source.zIndex[i] || 2,
                minZoom: Source.Zoom[i][0] || 1,
                maxZoom: Source.Zoom[i][1] || 20,
                source: new CWMS({
                    url: r,
                    params: {
                        id: Source.id,
                        LAYERS: Source.layers[i],
                        VERSION: Source.version || "1.1.0", //请求服务的版本
                        TRANSPARENT: true, //输出图像背景是否透明
                        STYLES: Source.styles,
                        FORMAT: Source.format,
                        maxZoom: Source.maxZoom,
                        width: Source.width,
                        height: Source.height,
                        VIEWPARAMS: Source.viewparams
                    }
                })
            })
        });
        return layers;
    }
    /**
     * XYZ 数据格式的参数
     * @param param0 
     * @returns 
     */
    TileXYZ = ({ Source }: Props) => {

        let layers: any = [];
        if (!Array.isArray(Source.url)) return layers;
        Source.url.forEach((r: any, i: any) => {
            layers[i] = new CTileLayer({
                zIndex: Source.zIndex || 2,
                source: new CXYZ({
                    url: r,
                })
            })
        });
        return layers;
    }
}

// Initialize the default map
const WMTS = () => {
    // Map coordinate constraints
    var projection = Cget(`EPSG:${4326}`);
    var projectionExtent = projection.getExtent();
    // Displays the constraint on the screen
    var size = CgetWidth(projectionExtent) / 256;
    var resolutions = [];
    var matrixIds = [];
    for (var z = 0; z < 20; ++z) {
        resolutions[z] = size / Math.pow(2, z);
        matrixIds.push(`GoogleCRS84Quad:${z}`)
    }
    return new CWMTS({
        url: CLOUD_API.wmsLocal,
        layer: "cloud_map:ls_map_grey",
        matrixSet: "GoogleCRS84Quad",
        format: "image/png",
        style: "",
        tileGrid: new CWMTSTileGrid({
            tileSize: [256, 256],
            extent: [-180.0, -269.99999999999994, 179.99999999999994, 90.0],
            origin: [-180.0, 90.0],
            resolutions: resolutions,
            matrixIds: matrixIds,
        }),
        wrapX: true
    })
}